package com.kcwl.carrier.domain.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kcwl.carrier.domain.bo.CarrierDetailInfoBO;
import com.kcwl.carrier.domain.entity.*;
import com.kcwl.carrier.domain.repository.ICarrierCompanyInfoRepository;
import com.kcwl.carrier.domain.repository.ICarrierCooperateInfoRepository;
import com.kcwl.carrier.domain.repository.ICarrierInfoRepository;
import com.kcwl.carrier.infrastructure.enums.CarrierUseErrorCodeEnum;
import com.kcwl.carrier.infrastructure.exceptions.CarrierUserException;
import com.kcwl.carrier.infrastructure.repository.po.CarrierCompanyInfoPO;
import com.kcwl.carrier.infrastructure.repository.po.CarrierInfoPO;
import com.kcwl.carrier.interfaces.dto.CooperateEnterpriseDTO;
import com.kcwl.carrier.interfaces.query.CarrierListQuery;
import com.kcwl.carrier.interfaces.query.UserIdQuery;
import com.kcwl.ddd.application.constants.IdentityTypeEnum;
import com.kcwl.ddd.application.constants.YesNoEnum;
import com.kcwl.ddd.domain.entity.KcPage;
import com.kcwl.framework.utils.KcBeanConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 刘旭超
 * @date 2022年07月11日 17:11
 * @Description
 */
@Service
public class CarrierInfoDomainService {

    @Autowired
    private ICarrierInfoRepository carrierInfoRepository;

    @Autowired
    private ICarrierCooperateInfoRepository carrierCooperateInfoRepository;

    @Autowired
    private ICarrierCompanyInfoRepository carrierCompanyInfoRepository;

    public CarrierInfo queryByUserId(Long userId) {
        return carrierInfoRepository.queryByUserId(userId);
    }

    public CarrierInfo queryByCarrierId(Long carrierId) {
        return carrierInfoRepository.queryByCarrierId(carrierId);
    }

    public CarrierCooperateInfo queryInitCooperateInfo(Long carrierId) {
        return carrierCooperateInfoRepository.queryInitByCarrierId(carrierId);
    }

    public CarrierCooperateInfo getByCarrierId(Long assistantId) {
        return carrierCooperateInfoRepository.getByCarrierId(assistantId);
    }

    public CarrierCooperateInfo getCooperateInfoById(Long id) {
        return KcBeanConverter.toBean(carrierCooperateInfoRepository.getById(id), CarrierCooperateInfo.class);
    }

    public int addCarrierInfo(CarrierInfo carrierInfo) {
        return carrierInfoRepository.insert(carrierInfo);
    }

    public int updateCarrierInfoById(CarrierInfo carrierInfo) {
        return carrierInfoRepository.updateById(carrierInfo);
    }

    /**
     * 保存运力辅助人初始合作企业信息
     */
    public int saveInitCooperate(CarrierCooperateInfo carrierCooperateInfo) {
        return carrierCooperateInfoRepository.saveInit(carrierCooperateInfo);
    }

    /**
     * 添加运力辅助人合作企业信息
     */
    public int addCooperate(CarrierCooperateInfo carrierCooperateInfo) {
        return carrierCooperateInfoRepository.add(carrierCooperateInfo);
    }

    /**
     * 更新运力辅助人合作企业信息
     */
    public int updateCooperateByCarrierId(CarrierCooperateInfo carrierCooperateInfo) {
        return carrierCooperateInfoRepository.updateByCarrierId(carrierCooperateInfo);
    }

    public int saveCompanyInfo(CarrierCompanyInfo carrierCompanyInfo) {
        return carrierCompanyInfoRepository.insert(carrierCompanyInfo);
    }

    public int updateCompanyByCarrierId(CarrierCompanyInfo carrierCompanyInfo) {
        return carrierCompanyInfoRepository.updateByCarrierId(carrierCompanyInfo);
    }

    /**
     * 查询物流公司企业信息
     */
    public CarrierCompanyInfo queryCompayInfoByCarrierId(Long carrierId) {
        return carrierCompanyInfoRepository.queryByCarrierId(carrierId);
    }

    /**
     * 司机详情查询
     */
    public CarrierDetailInfoBO queryCarrierDetailInfo(Long carrierId) {
        CarrierInfo carrierInfo = this.queryByCarrierId(carrierId);
        if (carrierInfo == null) {
            throw new CarrierUserException(CarrierUseErrorCodeEnum.CARRIER_IS_NOT_EXISTS);
        }
        CarrierDetailInfoBO carrierDetailInfoBO = new CarrierDetailInfoBO();
        carrierDetailInfoBO.setCarrierInfo(carrierInfo);
        if (IdentityTypeEnum.CARRIER_TCOM_MANAGER.getSubType().equals(carrierInfo.getIdentityType())) {
            CarrierCompanyInfo carrierCompanyInfo = this.queryCompayInfoByCarrierId(carrierId);
            carrierDetailInfoBO.setCarrierCompanyInfo(carrierCompanyInfo);
        } else if (IdentityTypeEnum.CARRIER_BRKR.getSubType().equals(carrierInfo.getIdentityType())) {
            CarrierCooperateInfo carrierCooperateInfo = this.queryInitCooperateInfo(carrierId);
            carrierDetailInfoBO.setCarrierCooperateInfo(carrierCooperateInfo);
        }

        return carrierDetailInfoBO;
    }

    /**
     * 个体司机审核信息列表查询
     */
    public KcPage<SingleCarrierListInfo> querySingleList(SingleCarrierInfoListCondition condition) {
        return carrierInfoRepository.querySingleList(condition);
    }

    /**
     * 企业司机审核信息列表查询
     */
    public KcPage<FirmCarrierListInfo> queryFirmList(FirmCarrierInfoListCondition condition) {
        return carrierInfoRepository.queryFirmList(condition);
    }

    public List<CarrierInfo> getByIds(List<Long> ids) {
        return carrierInfoRepository.getByIds(ids);
    }

    /**
     * 用户名下司机列表，只查询有效数据
     */
    public List<CarrierInfo> getCarrierDrivers(UserIdQuery query) {
        return carrierInfoRepository.getCarrierDrivers(query.getUserId(),query.getKeyWord());
    }


    public KcPage<CarrierList> getCarrierList(CarrierListQuery query) {
        return carrierInfoRepository.getCarrierList(query);
    }

    public KcPage<CooperateEnterpriseDTO> queryCooperateEnterpriseList(CooperateEnterpriseCondition condition) {
        return carrierInfoRepository.queryCooperateEnterpriseList(condition);
    }

    public CarrierInfo getCarrierInfo(CarrierInfo carrierInfo) {
        return carrierInfoRepository.getCarrierInfo(carrierInfo);
    }

    public List<DriverDispatchInfo> queryDispatchDriverInfo(String mobile) {
        return carrierInfoRepository.queryDispatchDriverInfo(mobile);
    }

    public List<DriverDispatchInfo> queryDispatchDriverAndVehicleInfo(String queryInfo) {
        return carrierInfoRepository.queryDispatchDriverAndVehicleInfo(queryInfo);
    }

    public List<CompanyDispatchInfo> queryDispatchCompanyInfo(String mobile) {
        return carrierInfoRepository.queryDispatchCompanyInfo(mobile);
    }

    public List<CompanyDispatchInfo> queryDispatchCompanyAndAuxiliaryInfo(String mobile) {
        return carrierInfoRepository.queryDispatchCompanyAndAuxiliaryInfo(mobile);
    }

    public List<CompanyDispatchInfo> queryDispatchAuxiliaryInfo(String mobile) {
        return carrierInfoRepository.queryDispatchAuxiliaryInfo(mobile);
    }

    public List<CompanyDispatchInfo> queryDispatchCarrierInfo(List<Long> ids) {
        return carrierInfoRepository.queryDispatchCarrierInfo(ids);
    }

    public List<Long> queryDispatchDriverOrg(Long userId) {
        return carrierInfoRepository.queryDispatchDriverOrg(userId);
    }
    public List<CarrierInfo> getCarrierListByParams(CarrierListQuery query) {
        return carrierInfoRepository.getCarrierListByParams(query);
    }

    public KcPage<TmsDriverInfo> queryTmsDriverInfoPage(TmsDriverInfoCondition tmsDriverInfoCondition) {
        return carrierInfoRepository.queryTmsDriverInfoPage(tmsDriverInfoCondition);
    }

    public void deleteTmsDriver(Long userId) {
        CarrierInfoPO carrierInfoPO = carrierInfoRepository.getById(userId);
        carrierInfoPO.setDeleted(YesNoEnum.YEA.getValue());
        carrierInfoPO.setUpdateTime(new Date());
        carrierInfoRepository.updateById(carrierInfoPO);
    }

    /**
     * 根据手机号集合查询
     * @param list
     * @return
     */
    public List<CarrierInfo> queryByMobile(List<String> list) {
        return carrierInfoRepository.queryByMobile(list);
    }

    /**
     * 根据姓名集合查询
     * @param list
     * @return
     */
    public List<CarrierInfo> queryByName(List<String> list) {
        return carrierInfoRepository.queryByName(list);
    }

    public List<CarrierCompanyInfo> queryCompanyInfoByCarrierIdList(List<Long> carrierIdList) {
        QueryWrapper<CarrierCompanyInfoPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("carrier_id", carrierIdList);
        return KcBeanConverter.toList(carrierCompanyInfoRepository.list(queryWrapper), CarrierCompanyInfo.class);
    }

}
